

#include"stdafx.h"
#include"math.h"
#include"conio.h"
#include"cv.h"
#include"highgui.h"
#include"stdio.h"
#include"VideoCapture.h"
#include <vector>


#define TRESH 50
#define STALL 1 
#define THRESHOLD_MAX 150
#define BELOW_RED_COLOR 0
#define THRESHOLD_MY 120
#define WHITE_COLOR 255


vector<CvPoint> findBlobs (IplImage *src) {
		IplImage* temp = cvCloneImage(src);

        CvMemStorage *mem_storage = cvCreateMemStorage(0);
        CvSeq* contours = NULL;

                // actually find the contours
        CvContourScanner scanner = cvStartFindContours(src, mem_storage, \
                sizeof(CvContour), CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);

                // filter/approximate the blobs
        CvSeq* c;
        int numCont = 0;
        while( (c = cvFindNextContour( scanner )) != NULL ) {
			    // polynomial approximation
                        CvSeq* c_new;

                        c_new = cvApproxPoly(c, sizeof(CvContour), mem_storage,
                                CV_POLY_APPROX_DP, 1, 0);

                        cvSubstituteContour( scanner, c_new );
                        numCont++;
        }
        contours = cvEndFindContours( &scanner );

                // find the blob centers
        int i;
        vector<CvPoint> centers;
        CvMoments moments;
        double M00, M01, M10;

        for(i=0, c=contours; c != NULL; c = c->h_next, i++) {
                cvContourMoments(c, &moments);

                M00 = cvGetSpatialMoment(&moments,0,0);
                M10 = cvGetSpatialMoment(&moments,1,0);
                M01 = cvGetSpatialMoment(&moments,0,1);

                // put in the output array
                centers.push_back(cvPoint((int)(M10/M00) + cvGetSize(src).width,
                        (int)(M01/M00) + cvGetSize(src).height));
        }

        // release stuff
        cvReleaseMemStorage(&mem_storage);

        cvReleaseImage(&temp);

        return centers;
	}


int main()

{

int i,j,k;

int height,width,step,channels;
CvMemStorage * g_storage;
CvSeq* contours = 0;

int stepr, channelsr;

int temp=0;

uchar *data,*datar;



i=j=k=0;

//cvNamedWindow("Device 0");
//VideoCapture capt(1,350,200);

CvCapture * capt;

capt = cvCreateCameraCapture(1);

cvSetCaptureProperty( capt, CV_CAP_PROP_FRAME_WIDTH, 480 );

cvSetCaptureProperty( capt, CV_CAP_PROP_FRAME_HEIGHT, 320 );

IplImage * frame = cvQueryFrame(capt);


//IplImage * frame = capt.CreateCaptureImage();

//IplImage *im=cvLoadImage("4.jpg",1);

//cvNamedWindow("original",CV_WINDOW_AUTOSIZE);



//IplImage *r=cvCreateImage(cvGetSize(im), 8, 1 );
IplImage *dft=cvCreateImage(cvGetSize(frame), 8, 1 );
IplImage *g=cvCreateImage(cvGetSize(frame), 8, 1 );
IplImage *gNew=cvCreateImage(cvGetSize(frame), 8, 1 );
//IplImage *newF=cvCreateImage(cvGetSize(im), 8, 1 );

//IplImage *b=cvCreateImage(cvGetSize(im), 8, 1 );
IplImage *result=cvCreateImage(cvGetSize(frame), 8, 1 );

/**if(im==NULL ) {
puts("unable to load the frame");exit(0);}
printf("frame loaded");
*/

//cvNamedWindow("Result",CV_WINDOW_AUTOSIZE);
char c=0;
while(c!='q') {
capt.waitFrame(frame);

//printf("frame loaded  %d",frame->colorModel);
//cvCopy(frame,im,
//printf("num %s \n",im->colorModel);
//cvCvtColor(frame,newF,CV_YCrCb2BGR);
//cvSplit(im,b,g,r,NULL);
//cvCvtColor(im,b,CV_RGB2GRAY);
//cvShowImage("AfterGrayScale",b);
//printf("num %s \n",b->colorModel);
//cvThreshold(b,r,THRESHOLD_MY, WHITE_COLOR,CV_THRESH_BINARY);
//cvShowImage("R",b);
//cvShowImage("newR",r);

cvShowImage("Lalala", frame);
cvCvtColor(frame,g,CV_RGB2GRAY);
//cvShowImage("AfterGrayScalefromVideo",g);
//cvThreshold(g,gNew,THRESHOLD_MAX,BELOW_RED_COLOR,CV_THRESH_TOZERO_INV);
cvShowImage("AfterThresholdTrunc",g);
cvThreshold(g,result,THRESHOLD_MY, WHITE_COLOR,CV_THRESH_BINARY);
cvShowImage("result",result);

/*
int count =0,sumX=0,sumY=0 
for(i=0;i<g->height;i++) 
   for(j-=0;j<g->width;j++) 
       if(   ( i , j ) belongs to the object in question) 
          { 
              sumX += i; 
              sumY += j; 
              count++; 
          } 

if(count) 
{ 
       centreOfMassX=sumX/count; 
       centreOfMassY=sumY/count; 
} 
*/

//cvFindContours(frame,,&co,88,1,2,cvPoint(0,0))
/*
	cvFindContours( frame, g_storage, &contours, 88,1,2,cvPoint(0,0));
if( contours )	cvDrawContours(frame,contours,cvScalarAll(255),cvScalarAll(255),100);
*/
//cvShowImage("dft",dft);

c=cvWaitKey(STALL);

//vector<CvPoint> points = findBlobs(result);
//printf("%d", points.size());

}

capt.stop();




/*If you want to use cvErode you may...*/

/*Destroying all the Windows*/

/*cvShowImage("original",frame);

cvShowImage("Result",result);

cvSaveImage("result.jpg",result);*/

//cvWaitKey(0);

cvDestroyWindow("RED");
cvDestroyWindow("Result");
return 0;

}